package com.hbx.residential_service.dao;

import com.hbx.residential_service.domain.House;
import com.hbx.residential_service.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface HouseDao {
    //查询所有房屋信息
    @Select("select * from houses")
    @Results({
            @Result(id=true,property = "houseId",column = "houseId"),
            @Result(property = "houseNum",column = "houseNum"),
            @Result(property = "houseBuild",column = "houseBuild"),
            @Result(property = "houseStatus",column = "houseStatus"),
            @Result(property = "houseDate",column = "houseDate"),
            @Result(property = "user",column = "houseUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById"))
    })
    public List<House> findAll() throws Exception;

    @Insert("insert into houses(houseNum,houseBuild,houseStatus,houseDate,houseUserId) VALUES(#{houseNum},#{houseBuild},#{houseStatus},#{houseDate},#{user.userid})")
    void save(House house);

    @Delete("delete from houses where houseId = #{houseIds}")
    void deleteHouse(int houseIds);

    @Select("select * from houses where houseId = #{houseId}")//这里要有result不然user类传输不进去
    @Results({
            @Result(id=true,property = "houseId",column = "houseId"),
            @Result(property = "houseNum",column = "houseNum"),
            @Result(property = "houseBuild",column = "houseBuild"),
            @Result(property = "houseStatus",column = "houseStatus"),
            @Result(property = "houseDate",column = "houseDate"),
            @Result(property = "user",column = "houseUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById"))
    })
    House findUpdHouseById(int houseId);

    @Update("update houses set houseNum=#{houseNum},houseBuild=#{houseBuild},houseStatus=#{houseStatus},houseDate=#{houseDate},houseUserId=#{user.userid} where houseId=#{houseId}")
    void UpdateById(House house);

    @Select("select * from houses where houseUserId in (select userid from owners where username like #{content})")//这里用in不用=因为=只能匹配一个id
    @Results({
            @Result(id=true,property = "houseId",column = "houseId"),
            @Result(property = "houseNum",column = "houseNum"),
            @Result(property = "houseBuild",column = "houseBuild"),
            @Result(property = "houseStatus",column = "houseStatus"),
            @Result(property = "houseDate",column = "houseDate"),
            @Result(property = "user",column = "houseUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById"))
    })
    List<House> fsByUsername(@Param("content") String content,@Param("page") int page, @Param("size") int size);
}
